#练习，九酷音乐
import requests
import random
import re
import time
agent1="Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36"

agent2="Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36"

agent3="Mozilla/5.0 (Linux; Android 6.0.1; OPPO A57 Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/63.0.3239.83 Mobile Safari/537.36 T7/10.13 baiduboxapp/10.13.0.10 (Baidu; P1 6.0.1)"

agent4="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400"

agent5="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
list1=[agent1,agent2,agent3,agent4,agent5]
agent=random.choice(list1)
header={
"user-Agent":agent,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Cookie": "ff=ok; tt=ok; cc=ok; pp=ok; tmp_addplay=; BAIDU_SSP_lcr=https://www.baidu.com/link?url=T4fvvpg5rmPxaS5eLL7MfC8IFroT1jTSDQZpKqPLpSy&wd=&eqid=dd092512000a9d5f000000025ff42854; Hm_lvt_a5de315acb973b8e6da83458c9e456d3=1609472703,1609836640; l_music=11417/1004523l_end; shows=ok; jk_ifplay=1; Hm_lpvt_a5de315acb973b8e6da83458c9e456d3=1609836726; jk_addplay=11417/1004523/",
"Host": "www.9ku.com",
"Referer": "http://www.9ku.com/css/style/base.css",
"Upgrade-Insecure-Requests": "1",
"Host": "www.9ku.com"
}
#http://www.9ku.com/laoge/500shou.htm
#歌曲url
#https:http://www.9ku.com/play/11417.htm
#http://mp3.9ku.com/hot/2004/07-13/11417.mp3
#http://mp3.9ku.com/hot/2005/06-23/66417.mp3
#http://mp3.9ku.com/hot/2005/03-01/64541.mp3
#http://mp3.9ku.com/m4a/86179.m4a
#http://www.9ku.com/html/playjs/1001/998378.js
songID=[]#存放歌曲Id
songName=[]#存放歌曲名字
url="http://www.9ku.com/music/t_m_hits.htm"  #歌单页面
html=requests.get(url,headers=header)
strr=html.text
# print(strr)
pat1=r'<input type="checkbox" value="(.*?)@" name="Url"'
pat2=r'">(.*?) </a>'
idlist=re.findall(pat1,strr)#获取ID
titlelist=re.findall(pat2,strr)#获取歌曲名字
songID.extend(idlist)#将获取到的Id放入id列表中
songName.extend(titlelist)#将获取的歌曲名字放入歌曲名字列表中
print(idlist)
print(titlelist)
#真正的歌曲url资源
# http://mp32.9ku.com/upload/128/2018/02/09/875689.mp3
# upload\/128\/2018\/02\/09\/875689.mp3
for i in range(0,len(songID)):
	urlID=[]#存放真正歌曲资源的部分url
	js="http://www.9ku.com/html/playjs/1001/"+str(songID[i])+".js"#存放资源地址的js页面
	js1=requests.get(js,headers=header)
	js2=js1.text
	# print(js2)
	pat3=r'"wma":"(.*?)","m4a"'
	urllist=re.findall(pat3,js2)
	try:
		url1=urllist[0]
		result_url=eval(repr(url1).replace('\\',''))#真正的歌曲url资源
		songname=songName[i]
		data=requests.get(result_url).content#变为二进制
		print("正在下载第",i+1,"首")
		# print(result_url)
		with open("D:\\Music\jiukumusic\\{}.mp3".format(songname),"wb") as f:
			f.write(data)
		time.sleep(0.5)
	except Exception as e:
		print(e)
